package com.nus;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 

import android.widget.Toast;
import com.nus.R;
import com.nus.parsers.ParserAPIs;
import com.nus.representations.IvleForum;
import com.nus.representations.IvleNews;

public class IvleForumDetailsActivity extends Activity {
	private ListView lv;
	private ArrayList<IvleForum> mListItem;
	private String lv_arr[];
	ArrayList<IvleForum> forum;
	protected TextView title;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.forumdetails);
		
		 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
		 title = (TextView) findViewById(R.id.titleheader);
		 title.setText("Forum");
		 ImageView b1 = (ImageView)findViewById(R.id.home);
		  b1.setImageDrawable(getResources().getDrawable(R.drawable.ivle));
		TextView tv1 = (TextView) findViewById(R.id.studentname);
	       tv1.setText(IvleForumActivity.forumdet.getStudentName());
	       //tv1.setTextColor(Color.BLUE);
		TextView tv2 = (TextView) findViewById(R.id.title);
		tv2.setText(IvleForumActivity.forumdet.getTitle());
		TextView tv3 = (TextView) findViewById(R.id.time);
		tv3.setText(IvleForumActivity.forumdet.getDateText());
		//tv3.setTextColor(Color.BLUE);
		TextView tv4 = (TextView) findViewById(R.id.body);
		tv4.setText(Html.fromHtml(IvleForumActivity.forumdet.getBody()));
		Button b = (Button) findViewById(R.id.reply);
		b.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				AlertDialog.Builder alert = new AlertDialog.Builder(IvleForumDetailsActivity.this);

				alert.setTitle("Post Reply");
				alert.setMessage(IvleForumActivity.forumdet.getTitle());

				// Set an EditText view to get user input 
				final EditText input = new EditText(IvleForumDetailsActivity.this);
				alert.setView(input);

				alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
				  String value = input.getText().toString();
				  // Do something with value!
					int TIMEOUT_MILLISEC = 10000;  // = 10 seconds
					HttpParams httpParams = new BasicHttpParams();
					HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
					HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
					HttpClient client = new DefaultHttpClient(httpParams);
					JSONObject json = new JSONObject();

					HttpPost request = new HttpPost("https://ivle.nus.edu.sg/api/Lapi.svc/Forum_ReplyThread_JSON");
					StringEntity se = null;
				try {
					json.put("APIKey", "W7RtgW7GZKUpaEkJ1NwUD");
					json.put("AuthToken", NUS_Android.IVLE_Token);
					//75a5fa68-0cf4-4acb-9aeb-96dd52364fe3
					json.put("ThreadID",IvleForumActivity.forumdet.getID() );
					json.put("Title", "Re:"+IvleForumActivity.forumdet.getTitle());
					json.put("Reply", value);
					se = new StringEntity(json.toString());  
				} catch (JSONException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				se.setContentEncoding("UTF-8");

				se.setContentType("application/json");
				request.setEntity(se);

					
//					se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
		//
//					request.setEntity(se);
				try {
					HttpResponse response = client.execute(request);
					if(response!=null){
		                InputStream in = response.getEntity().getContent(); //Get the data in the entity

		        }

				} catch (ClientProtocolException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

					
					
			
					
					//Toast.makeText(this, "You pressed the icon! " + "For More tutorials come to www.firstdroid.com", Toast.LENGTH_SHORT).show();
		          puttoast();
				  }
				});
				//Toast.makeText(IvleForumDetailsActivity.this, "Success! the post is created", Toast.LENGTH_SHORT).show();
				
				alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
				  public void onClick(DialogInterface dialog, int whichButton) {
				    // Canceled.
				  }
				});

				alert.show();
			}
		});
	}
	private void puttoast() {
		// TODO Auto-generated method stub
		Toast.makeText(this, "Success! the post is created",
				Toast.LENGTH_LONG).show();
	}

}
